﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IForm_EditSettings</summary>
    public class IForm_EditSettings : IForm
    {
        public ISettings Value; // Objekt na editaciu
        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_Default, FBtn_OK, FBtn_Cancel;
        protected IComboBox FCombo_Groups;
        protected IListView FList;

        /// <summary>Konstruktor</summary>
        public IForm_EditSettings()
        {
            Value = new ISettings(); // Vytvorim objekt
        }
        /// <summary>Dispose metoda</summary>
        /// <param name="disposing">parameter</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing); // Volam predchodcu
            Value = (ISettings)ICore.DisposeObject(Value); // Zrusim objekt
        }

        /// <summary>Metoda OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowInTaskbar = false; MinimizeBox = MaximizeBox = false; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; // Inicializacia vlastnosti

            Text = lang.FORM_EDIT_SETTINGS_TEXT_4;
            SetBounds(0, 0, 700, 500); System.Drawing.Size CS = ClientSize;

            FGrp = new IGroupBox(); // Vytvorim groupbox
            FBtn_Default = new IButton(lang.FORM_EDIT_SETTINGS_TEXT_1, Event_DefaultBtnClick);
            FBtn_OK = new IButton(lang.FORM_EDIT_SETTINGS_TEXT_2); 
            FBtn_Cancel = new IButton(lang.FORM_EDIT_SETTINGS_TEXT_3);

            FCombo_Groups = new IComboBox(); // Vytvorim combobox
            for (int i = 0; i < ISettings.SETTINGS_GROUP_LAST; i++) FCombo_Groups.AddItem(ISettings.GroupID2Str(i), i);

            FList = new IListView(); FList.HideSelection = false; FList.FullRowSelect = true; FList.ShowItemToolTips = true; // Vytvorim listview
            FList.CreateColumns(new string[] { lang.FORM_EDIT_SETTINGS_TEXT_5_1, lang.FORM_EDIT_SETTINGS_TEXT_5_2 }, new int[] { 400, 200 }, new System.Windows.Forms.HorizontalAlignment[] { System.Windows.Forms.HorizontalAlignment.Left, System.Windows.Forms.HorizontalAlignment.Center });

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FCombo_Groups, FList }); // Zaradim
            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_Default, FBtn_OK, FBtn_Cancel }); // Zaradim

            IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50); // Umiestnim prvky do okna
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FBtn_Default, FGrp, 0, 10);
            IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10);
            IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);
            IControl.PlaceControl_Inside_LeftTop_LeftTop(FCombo_Groups, FGrp, 4, 10); FCombo_Groups.Width = FGrp.ClientSize.Width - 8;
            FList.Width = FCombo_Groups.Width; IControl.PlaceControl_Outside_LeftTop_LeftBottom(FList, FCombo_Groups, 0, 4); FList.Height = FGrp.ClientSize.Height - FCombo_Groups.Bottom - 8;

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus(); FBtn_OK.DialogResult = System.Windows.Forms.DialogResult.OK;

            // Nastavim eventy
            FCombo_Groups.SelectedIndexChanged += Event_SelectedIndexChanged;
            FList.ItemActivate += Event_ItemActivate;

            CreateList(ISettings.SETTINGS_GROUP_COMMON);
            return true; // Vsetko OK
        }
        /// <summary>Vytvorenie zoznamu podla zadanej skupiny</summary>
        /// <param name="pGroupID">ID skupiny</param>
        protected void CreateList(int pGroupID)
        {
            FCombo_Groups.SelectedIntID = pGroupID; // Nastavim skupinu v comboboxe

            FList.BeginCreate(); // Zacinam vytvarat obsah

            for (int i = 0; i < ISettings.SETTINGS_ID_LAST; i++) // Prejdem vsetky funkcie
            {
                if (ISettings.ID2GroupID(i) != pGroupID) continue;
                FList.AddItem(ISettings.SubgroupID2Str(ISettings.ID2SubroupID(i)), ISettings.ID2Str(i), i);
            }

            FList.EndCreate(-1); // Koniec vytvarania
            UpdateList(); // Doplnim hodnoty
        }
        /// <summary>Aktualizacia zoznamu</summary>
        protected void UpdateList()
        {
            foreach (IListViewItem LI in FList.Items) // Prejdem zaznamy
            {
                LI[1] = GetValue(LI.IntTag);
            }
        }
        /// <summary>Ziskanie aktualnej hodnoty nastavenia ako textovy popis</summary>
        /// <param name="pID">ID nastavenia</param>
        /// <returns>textovy popis</returns>
        protected string GetValue(int pID)
        {
            switch (pID)
            {
                case ISettings.SETTINGS_ID_APP_ONLY_ONE_INSTANCE: return ICore.Bool2Str(Value.App_OnlyOneInstance);
                case ISettings.SETTINGS_ID_APP_SHOW_QUIT_QUESTION: return ICore.Bool2Str(Value.App_ShowQuitQuestion);
                case ISettings.SETTINGS_ID_APPFORM_MAXIMIZE_ON_START: return ICore.Bool2Str(Value.AppForm_ForceMaximize);
                case ISettings.SETTINGS_ID_APPFORM_REMEMBER_LAST_POSITION: return ICore.Bool2Str(Value.AppForm_RememberLastPosition);
                case ISettings.SETTINGS_ID_APPFORM_MINIMIZE_TO_TRAY_ICON: return ICore.Bool2Str(Value.AppForm_MinimizeToTry);
                case ISettings.SETTINGS_ID_APPFORM_TAB_POSITION: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_11_4, Value.AppForm_TabPosition);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_DISPLAY_SIZE_MODE: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_12_4, Value.FileExplorer_DisplaySizeMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_DISPLAY_ICON_MODE: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_13_4, Value.FileExplorer_DisplayIconMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_DIRS_IN_BRACES: return ICore.Bool2Str(Value.FileExplorer_DirsInBraces);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_SHOW_ADMIN_SHARES: return ICore.Bool2Str(Value.FileExplorer_ShowAdminShares);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_MOVE_TO_NEXT_AFTER_SELECT: return ICore.Bool2Str(Value.FileExplorer_MoveToNextAfterSelect);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_AUTO_RELOAD_AFTER_CHANGE: return ICore.Bool2Str(Value.FileExplorer_AutoReloadAfterChange);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_SHOW_ITEM_MENU_MODE: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_18_4, Value.FileExplorer_ShowItemMenuMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_ITEM_TOOL_TIP_MODE: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_19_4, Value.FileExplorer_ItemToolTipMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_SELECT_AFTER_CALCULATE_FOLDER_SIZE: return ICore.Bool2Str(Value.FileExplorer_SelectAfterCalculateFolderSize);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_COPY_MODE: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_21_4, Value.FileExplorer_CopyItemMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_MOVE_MODE: return ICore.GetSubstring(lang.FORM_EDIT_SETTINGS_TEXT_22_4, Value.FileExplorer_MoveItemMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_COPY: return ICore.Bool2Str(Value.FileExplorer_ConfirmCopy);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_MOVE: return ICore.Bool2Str(Value.FileExplorer_ConfirmMove);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_DELETE: return ICore.Bool2Str(Value.FileExplorer_ConfirmDelete);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_DELETE_TO_TRASH: return ICore.Bool2Str(Value.FileExplorer_ConfirmMoveToTrash);
                case ISettings.SETTINGS_ID_APP_AUTO_SHOW_UPDATE_AVAILABLE: return ICore.Bool2Str(Value.App_AutoShowUpdateAvailable);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_EXTERNAL_EDITOR: return Value.FileExplorer_ExternalEditor;
                case ISettings.SETTINGS_ID_FILE_EXPLORER_EXTERNAL_VIEWER: return Value.FileExplorer_ExternalViewer;
                default: return "";
            }
        }
        /// <summary>Zmena hodnoty uzivatelom</summary>
        /// <param name="pID">ID nastavenia</param>
        /// <returns>true / false</returns>
        protected bool EditValue(int pID)
        {
            switch (pID)
            {
                case ISettings.SETTINGS_ID_APP_SHOW_QUIT_QUESTION: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_6_1, lang.FORM_EDIT_SETTINGS_TEXT_6_2, ref Value.App_ShowQuitQuestion);
                case ISettings.SETTINGS_ID_APPFORM_MAXIMIZE_ON_START: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_7_1, lang.FORM_EDIT_SETTINGS_TEXT_7_2, ref Value.AppForm_ForceMaximize);
                case ISettings.SETTINGS_ID_APPFORM_REMEMBER_LAST_POSITION: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_8_1, lang.FORM_EDIT_SETTINGS_TEXT_8_2, ref Value.AppForm_RememberLastPosition);
                case ISettings.SETTINGS_ID_APPFORM_MINIMIZE_TO_TRAY_ICON: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_9_1, lang.FORM_EDIT_SETTINGS_TEXT_9_2, ref Value.AppForm_MinimizeToTry);
                case ISettings.SETTINGS_ID_APP_ONLY_ONE_INSTANCE: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_10_1, lang.FORM_EDIT_SETTINGS_TEXT_10_2, ref Value.App_OnlyOneInstance);
                case ISettings.SETTINGS_ID_APPFORM_TAB_POSITION: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_11_1, lang.FORM_EDIT_SETTINGS_TEXT_11_2, lang.FORM_EDIT_SETTINGS_TEXT_11_3, ref Value.AppForm_TabPosition);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_DISPLAY_SIZE_MODE: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_12_1, lang.FORM_EDIT_SETTINGS_TEXT_12_2, lang.FORM_EDIT_SETTINGS_TEXT_12_3, ref Value.FileExplorer_DisplaySizeMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_DISPLAY_ICON_MODE: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_13_1, lang.FORM_EDIT_SETTINGS_TEXT_13_2, lang.FORM_EDIT_SETTINGS_TEXT_13_3, ref Value.FileExplorer_DisplayIconMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_DIRS_IN_BRACES: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_14_1, lang.FORM_EDIT_SETTINGS_TEXT_14_2, ref Value.FileExplorer_DirsInBraces);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_SHOW_ADMIN_SHARES: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_15_1, lang.FORM_EDIT_SETTINGS_TEXT_15_2, ref Value.FileExplorer_ShowAdminShares);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_MOVE_TO_NEXT_AFTER_SELECT: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_16_1, lang.FORM_EDIT_SETTINGS_TEXT_16_2, ref Value.FileExplorer_MoveToNextAfterSelect);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_AUTO_RELOAD_AFTER_CHANGE: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_17_1, lang.FORM_EDIT_SETTINGS_TEXT_17_2, ref Value.FileExplorer_AutoReloadAfterChange);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_SHOW_ITEM_MENU_MODE: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_18_1, lang.FORM_EDIT_SETTINGS_TEXT_18_2, lang.FORM_EDIT_SETTINGS_TEXT_18_3, ref Value.FileExplorer_ShowItemMenuMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_ITEM_TOOL_TIP_MODE: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_19_1, lang.FORM_EDIT_SETTINGS_TEXT_19_2, lang.FORM_EDIT_SETTINGS_TEXT_19_3, ref Value.FileExplorer_ItemToolTipMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_SELECT_AFTER_CALCULATE_FOLDER_SIZE: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_20_1, lang.FORM_EDIT_SETTINGS_TEXT_20_2, ref Value.FileExplorer_SelectAfterCalculateFolderSize);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_COPY_MODE: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_21_1, lang.FORM_EDIT_SETTINGS_TEXT_21_2, lang.FORM_EDIT_SETTINGS_TEXT_21_3, ref Value.FileExplorer_CopyItemMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_MOVE_MODE: return IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SETTINGS_TEXT_22_1, lang.FORM_EDIT_SETTINGS_TEXT_22_2, lang.FORM_EDIT_SETTINGS_TEXT_22_3, ref Value.FileExplorer_MoveItemMode);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_COPY: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_23_1, lang.FORM_EDIT_SETTINGS_TEXT_23_2, ref Value.FileExplorer_ConfirmCopy);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_MOVE: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_24_1, lang.FORM_EDIT_SETTINGS_TEXT_24_2, ref Value.FileExplorer_ConfirmMove);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_DELETE: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_25_1, lang.FORM_EDIT_SETTINGS_TEXT_25_2, ref Value.FileExplorer_ConfirmDelete);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_CONFIRM_DELETE_TO_TRASH: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_26_1, lang.FORM_EDIT_SETTINGS_TEXT_26_2, ref Value.FileExplorer_ConfirmMoveToTrash);
                case ISettings.SETTINGS_ID_APP_AUTO_SHOW_UPDATE_AVAILABLE: return IApp.ShowEditor_Boolean(Text, lang.FORM_EDIT_SETTINGS_TEXT_27_1, lang.FORM_EDIT_SETTINGS_TEXT_27_2, ref Value.App_AutoShowUpdateAvailable);
                case ISettings.SETTINGS_ID_FILE_EXPLORER_EXTERNAL_EDITOR:
                    {
                        string FN = IApp.ShowEditor_ChooseFileLoad(lang.FORM_EDIT_SETTINGS_TEXT_28_1, lang.FORM_EDIT_SETTINGS_TEXT_28_3);
                        if (FN != "") Value.FileExplorer_ExternalEditor = FN;
                        return FN != "";
                    }
                case ISettings.SETTINGS_ID_FILE_EXPLORER_EXTERNAL_VIEWER:
                    {
                        string FN = IApp.ShowEditor_ChooseFileLoad(lang.FORM_EDIT_SETTINGS_TEXT_28_2, lang.FORM_EDIT_SETTINGS_TEXT_28_3);
                        if (FN != "") Value.FileExplorer_ExternalViewer = FN;
                        return FN != "";
                    }
                default: return false;
            }
        }

        /// <summary>Event: SelectedIndexChanged</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_SelectedIndexChanged(object pSender, EventArgs e)
        {
            CreateList(FCombo_Groups.SelectedIntID); // Volam zmenu skupiny
        }
        /// <summary>Event: ItemActivate</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_ItemActivate(object pSender, EventArgs e)
        {
            int ID = FList.SelectedIntTag; // Ziskam ID funkcie
            if (ID == -1) return; // Ziadna?
            if (EditValue(ID)) UpdateList(); // Volam zmenu skratky a aktualizaciu zoznamu
        }
        /// <summary>Event: Click pre tlacidlo 'Prednastavene'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_DefaultBtnClick(object pSender, EventArgs e)
        {
            if (IApp.ShowQuestion(lang.FORM_EDIT_SETTINGS_TEXT_6)) // Potvrdim
            {
                Value.LoadDefaults(); // Nacitam prednastavene
                UpdateList(); // Aktualizujem zoznam
            }
        }
    }
}
